package com.xbn.netty.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author XBN
 * @date 2020/10/13 16:23
 * @email 78742541@qq.com
 */
/*
FileChannelWrite write  demo
*/
public class FileChannelWrite {
    public static void main(String[] args) throws IOException {
        String str="hello";
        //创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("L:\\tempFile\\channel.txt");
        //通过FileOutputStream获取对应的FileChannel,真实类型是FileChannelImpl
        java.nio.channels.FileChannel fileChanne = fileOutputStream.getChannel();
        //创建一个缓冲区ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放入到byteBuffer中去
        byteBuffer.put(str.getBytes());
        //读取，需要反转缓冲区
        byteBuffer.flip();
        //将byteBuffer的数据写入到channel中去
        fileChanne.write(byteBuffer);
        fileOutputStream.close();
    }
}
